package com.masz.seataone.service;

import com.alibaba.fastjson.JSONObject;
import com.masz.seataone.dao.mapper.TbSeata;
import com.masz.seataone.dao.mapper.TbSeataMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

/**
 * @author masz
 * @Date 2022/2/12 14:08
 */
@Service
public class SeataOneService {

    @Autowired
    TbSeataMapper mapper;

    @Autowired
    RestTemplate restTemplate;

    @Transactional(rollbackFor = Exception.class)
    public void test(String v){
        TbSeata s = new TbSeata();
        s.setV(v);
        mapper.insert(s);
    }

    /**
     * TA 模式
     * @param v
     */
    @GlobalTransactional(rollbackFor = Exception.class)
    public void ta(String v) {
        TbSeata s = new TbSeata();
        s.setV(v);
        mapper.insert(s);

        rcpSeataTwo(v);
//        rcpSeataThree(v);
    }

    public void rcpSeataTwo(String v){
        JSONObject date = new JSONObject();
        date.put("v", v + "-two");
        restTemplate.postForEntity("http://seata-two/seata/test", date,String.class);
    }

    public void rcpSeataThree(String v){
        JSONObject date = new JSONObject();
        date.put("v", v + "-three");
        restTemplate.postForEntity("http://seata-three/seata/test", date,String.class);
    }


}
